#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from collections import Iterable
from functools import reduce


# 1.map函数和js中的map函数类似，只不过写法上有些出入
def f(x):
    return x * x

print(isinstance(map(f, [1, 2, 3, 4, 5, 6, 7]),Iterable)) # True

#   map 将一个函数作用于一个Iterable对象
print(list(map(f, [1, 2, 3, 4, 5, 6, 7]))) # [1, 4, 9, 16, 25, 36, 49]
print(list(map(str, [1, 2, 3, 4, 5, 6, 7]))) # ['1', '2', '3', '4', '5', '6', '7']

# 2. reduce也是将一个函数作用于Iterable对象,而且这个函数必须接收两个参数
# reduce把结果继续和序列的下一个元素做计算
def add(x,y):
    return x * y

print(reduce(add,[1,2,3,4,5])) # 120
print(reduce(add,map(f, [1, 2, 3, 4, 5, 6, 7]))) # map是一个Iteratable对象 25401600


# 案例联系
def addStr(x,y):
    return x + y

def up(name):
    x = name[:1]
    y = name[1:len(name)]
    zm = {'a':'A','b':'B','c':'C','d':'D'}
    return reduce(addStr,[zm[x],y])

def normalize(name):
    return up(name.lower())

print(normalize('acb')) # Acb
print(normalize('aCb')) # Acb
